<script type="text/javascript">
$(document).ready(function()
{
	$('#sortable_questiongroups_<?php echo $assessment['Assessment']['id']?>').sortable({

	   stop: function(event, ui) {		   	
			var questiongroup_order_array = $( "#sortable_questiongroups_<?php echo $assessment['Assessment']['id']?>" ).sortable("toArray");
			$.post("/questiongroups/reorder/", { 'data[questiongroups]': [questiongroup_order_array] });
	   }

	});
});
</script>
<div class="aToolbar">
	<span class="aToolbarButton">
		<span class="iconNew"></span>
		<?php
			echo $ajax->link(
				'Add A Question Group',
				array( 'controller' => 'questiongroups', 'action' => 'ajax_add', $assessment['Assessment']['id'] ),
				array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'indicator' => 'loading', 'title' => 'Add Question Group')
			);
		?>
	</span>
	<span class="aToolbarButton">
		<span class="iconEdit"></span>
		<?php 
			echo $ajax->link
			(
				'Edit Assessment',
				array( 'controller' => 'assessments', 'action' => 'edit', $assessment['Assessment']['id'] ),
				array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'indicator' => 'loading')
			);
		?>
	</span>
	<span class="aToolbarButton">
		<span class="iconDelete"></span>
		<?php 
			echo '<a href="/assessments/delete/'.$assessment['Assessment']['id'].'">Delete Assessment</a>';
		?>
	</span>
	<span class="aToolbarButton">
		<span class="iconDuplicate"></span>
		<?php 
			echo '<a href="/assessments/duplicate/'.$assessment['Assessment']['id'].'">Duplicate Assessment</a>';
		?></a>
	</span>
</div>
<div class="aTable">
	<div class="aTableHeader">
		<span class="aTableDelete">Delete</span>
		<span class="aTableDuplicate">Duplicate</span>
		<span class="aTableEdit">Edit</span>
		<span class="aTableTitle">Question Group Title</span>
		<span class="aTableQuestion">Question</span>
	</div>
<?php 
	$questiongroups = '';
	$i = 0;//switch between 1 and 0 to toggle between the two list classes
	foreach ($assessment['Questiongroup'] as $questiongroup){

		if (strlen($questiongroup['name']) > 40)
		{
			$questiongroup['name'] = substr($questiongroup['name'],0,40);
			$questiongroup['name'] .= '..';
		}
		$question_get_ajax = $ajax->link(
			$questiongroup['name'],
			array( 'controller' => 'questions', 'action' => 'ajax_get', $questiongroup['id'] ),
			array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'indicator' => 'loading_question_big')
		);

		$questiongroup_delete_ajax = $ajax->link(
			'[x]',
			array( 'controller' => 'questiongroups', 'action' => 'ajax_delete', $questiongroup['id'] ),
			array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'confirm' => 'Are you sure?', 'indicator' => 'loading_questiongroup')
		);

		$questiongroup_delete_ajax_img = str_replace('[x]','<span class="iconDelete"></span>',  $questiongroup_delete_ajax);

		$questiongroup_duplicate_ajax = $ajax->link(
			'[x]',
			array( 'controller' => 'questiongroups', 'action' => 'ajax_duplicate', $questiongroup['id'].'/'.$assessment['Assessment']['id'] ),
			array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'indicator' => 'loading_questiongroup')
		);

		$questiongroup_duplicate_ajax_img = str_replace('[x]','<span class="iconDuplicate"></span>',  $questiongroup_duplicate_ajax);
		
		$questiongroup_edit_ajax = $ajax->link(
			'[x]',
			array( 'controller' => 'questiongroups', 'action' => 'ajax_edit', $questiongroup['id'] ),
			array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'indicator' => 'loading_edit')
		);

		$questiongroup_edit_ajax_img = str_replace('[x]','<span class="iconEdit"></span>',  $questiongroup_edit_ajax);
		
		$questiongroup_questions = $ajax->link(
			'[x]',
			array( 'controller' => 'questions', 'action' => 'ajax_get', $questiongroup['id'] ),
			array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'indicator' => 'loading_edit')
		);

		$questiongroup_questions_img = str_replace('[x]','<span class="iconQuestion"></span>',  $questiongroup_questions);

		if($i == 0)
		{
			$questiongroups .=  '<div class="aTableRowA" id="'.$questiongroup['id'].'">';
			$i = 1;
		}
		else
		{
			$questiongroups .=  '<div class="aTableRowB" id="'.$questiongroup['id'].'">';
			$i = 0;
		}
		
		$questiongroup_name = $questiongroup['name'];
		if(strlen($questiongroup_name > 40))
		{
			$questiongroup_name = substr($questiongroup_name, 0, 40);
			$questiongroup_name .= "...";
		}
		$questiongroups .=  
		'
			<li id="'.$questiongroup['id'].'">
				<span class="aTableDelete">'.$questiongroup_delete_ajax_img.'</a></span>
				<span class="aTableDuplicate">'.$questiongroup_duplicate_ajax_img.'</span>
				<span class="aTableEdit">'.$questiongroup_edit_ajax_img.'</span>
				<span class="aTableTitle">'.$questiongroup_name.'</span>
				<span class="aTableQuestion">'.$questiongroup_questions_img.'</span>
			</li>
			</div>
		';

	}
	echo '<ul id="sortable_questiongroups_'.$assessment['Assessment']['id'].'">
			'.$questiongroups.'
		  </ul>';
?>